<?php
	// Load variables.
		
	$Product	=	md_RequestParam('product');
	$Installer	=	md_RequestParam('installer');
	$Username	=	md_RequestParam('username');
	$Password	=	md_RequestParam('password');
		
	if ( isset($Password) )				{ $Password = sha1($Password); }
	
	if ( !isset($Username) || $Username == "")
	{
		mis_SendFunctionDie('Error',array('The Username recieved by the content servers was invalid or missing.'));
	}
	if ( !isset($Product) || $Product == "")
	{
		mis_SendFunctionDie('Error',array('The product requested was invalid or missing.'));
	}	
	if ( md_IsValidProductName($Product) == false )
	{
		mis_SendFunctionDie('Error',array('The product requested could not be served because its name was not a valid product name.'));
	}
	
	if ( $md_db )
	{
		$register_query = sprintf("SELECT COUNT(register.username) as AlreadyRegistered FROM ".md_GetDatabaseName('register')." register, ".md_GetDatabaseName('products')." product WHERE register.username = '%s' AND product.name = '%s' and register.product = product.id", md_SlashGlobalVar($Username), md_SlashGlobalVar($Product));
		$register_slot=md_sql($register_query);
		
		if ($md_sql_row[$register_slot]['AlreadyRegistered'] == 0)
		{
			// Only do this if the user isn't registered as having installed the game
			// Check if the product is password protected, if it is, check for the correct password
			
			$slot	=	md_sql('SELECT * FROM '.md_GetDatabaseName('products').' WHERE name = "'.md_SlashGlobalVar($Product).'"');
			
			if ( $md_sql_total[$slot] )
			{
				// The product requested was found in our database.
				
				// See if the product currently is password protected.
				if ( md_date2stamp($md_sql_row[$slot]['unlock']) > time() )
				{
					if ( isset($Password ) )
					{
						if ( strcmp(sha1($md_sql_row[$slot]),$Password) != 0 )
						{
							mis_SendFunctionDie('PasswordIncorrect',array());							
						}
					}
					else
					{
						mis_SendFunctionDie('PasswordIncorrect',array());
					}
				}
			}
			else
			{				
				mis_SendFunctionDie('Error',array('The product requested ('.$Product.') is not supported by this content server at the moment. (No database entry)'));
			}
			
			$Date		=	md_Stamp2Date(time());
			$query		=	sprintf("INSERT INTO ".md_GetDatabaseName('register')." (username, product, installed) VALUES ('%s', %s, '%s')",
								   md_SlashGlobalVar($Username),
								   $md_sql_row[$slot]['id'],
								   md_SlashGlobalVar($Date));
								   
			md_clearslot($slot);
		
			md_sql_once($query);
		}
		md_clearslot($register_slot);
	}
	else
	{
		// Todo: We need support for content servers without databases
	}
	
	// Is the installer outdated?
	$NeedsNewInstaller	=	false;
	
	
	// Detect if we need to update the installer
	if ( isset($Installer) )
	{
		$InstallerParameters	=	explode(';',$Installer);
		$InstallerArray			=	array();
		
		for ( $I = 0; $I < count($InstallerParameters); $I++ )
		{
			$ParameterParameters	=	explode('=',$InstallerParameters[$I]);
			if ( count($ParameterParameters) == 1 )
			{
				$InstallerArray[strtolower($ParameterParameters[0])]	=	'';				
			}
			else
			{
				$InstallerArray[strtolower($ParameterParameters[0])]	=	$ParameterParameters[1];
			}		
		}
		
		if ( isset( $md_CurrentInstallerVersions ) )
		{
			if ( isset( $md_CurrentInstallerVersions[$InstallerArray['distribution']] ) &&
				 isset($InstallerArray['version']) )
			{
				$NeedsNewInstaller	=	true;
				for ( $I = 0; $I < count($md_CurrentInstallerVersions[$InstallerArray['distribution']]); $I++ )
				{
					if ( strcasecmp( $md_CurrentInstallerVersions[$InstallerArray['distribution']][$I],$InstallerArray['version']) == 0 )
					{
						$NeedsNewInstaller	=	false;									
					}
				}
			}
		}		
	}
	
	// Ensure the newest installer is installed
	if ( $NeedsNewInstaller )
	{
		mis_SendFunctionDie('MaxsiDistributionUpdateAvailable',array());
	}
	else
	{
		mis_SendFunctionDie('SuccessfullyRegistered',array());		
	}
?>